﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace ToDoList.Logic
{

    /// <summary>
    /// 事项功能
    /// </summary>
    public class EventFunction
    {
        private static TodoContext _ctx = new TodoContext();
        /// <summary>
        ///添加待办事项,输入清单号，添加待办事件的ID，待办事件，结束时间，和备注
        /// 成功返回true
        /// </summary>
        /// <param name="lsitID"></param>
        /// <param name="content"></param>
        /// <param name="overTime"></param>
        /// <param name="reamark"></param>
        public bool AddEvent(int userId, int listId,string content, DateTime overTime, string reamark)
        {
            if (_ctx.Matters.Any(prop => prop.InventoryId == listId && prop.UserId == userId ))
            {
               Matters userBcklogThing = new Matters()
                { 
                   MatterName = content ,
                  InventoryId  = listId,
                    UserId  = userId,
                    CreationTime  = DateTime.Now,
                    Status = false,
                   OverTime = overTime,
                    Reamrk = reamark,
                };
                _ctx.Matters.Add(userBcklogThing);
                _ctx.SaveChanges();
                return true;
            }
            return false;
        }
        /// <summary>
        ///删除事项，传入事项的ID,如果成功返回一个true
        /// </summary>
        /// <param name="content"></param>
        public void  DeleteEvent(int events)
        {
                var backlog = _ctx.Matters.SingleOrDefault(item => item.Id == events);
                _ctx.Matters.Remove(backlog);
                _ctx.SaveChanges();

        }
        /// <summary>
        /// 查看所有事项
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matters> GetAllEvents(int inventoryId,int userId)
        {
            return _ctx.Matters.Where(prop => prop.UserId == userId&&prop.InventoryId ==inventoryId ).ToList();
        }
        /// <summary>
        /// 获取未完成事项
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matters> GetUnfinishedEvent(int inventoryId,int userId) {
            return  _ctx.Matters.Where(prop => prop.UserId == userId && prop.Status ==false && prop.InventoryId == inventoryId).ToList();
        }

        /// <summary>
        ///  //添加过期时间
        /// </summary>
        /// <param name="backlogId"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public bool AddOverTime(int backlogId, DateTime time)
        {
            var result = FindEvent(backlogId);
            if (result != null)
            {
                result.OverTime = time;
                _ctx.SaveChanges();
                return true;
            }
                return false;
        }
        /// <summary>
        ///  //添加备注
        /// </summary>
        /// <param name="backlogId"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public bool AddRemark(int backlogId, string remark)
        {
            var result = FindEvent(backlogId);
            if (result != null)
            {
                result.Reamrk = remark;
                _ctx.SaveChanges();
                return true;
            }
                return false;
        }
        /// <summary>
        /// 查找事件
        /// </summary>
        /// <param name="backlogId"></param>
        /// <returns></returns>
        public Matters FindEvent(int backlogId)
        {
            return _ctx.Matters.SingleOrDefault(prop => prop.Id == backlogId);
        }
        /// <summary>
        /// ///完成某一待办事项,成功返回true
        /// </summary>
        /// <param name="state"></param>
        /// <param name="backlogId"></param>
        /// <returns></returns>
        public bool ChangeEventSate(bool state,int backlogId) {
            var result = FindEvent(backlogId);
            if (result != null)
            {
                result.Status = state;
                _ctx.SaveChanges();
                return true;
            }
          
            return false;
        }
    }
}
